<include file="Public/head"/>
<script src="{:get_style_cdnurl()}paimai/yunying/js/city.js" type="text/javascript"></script>
<style type="text/css">
    .branch_company, .man_id {
        display: block;
        text-align: center;
        width: 300px;
        background: #fff url("{:get_style_cdnurl()}paimai/yunying/images/sanjiao.jpg") no-repeat scroll 280px 7px;
        border: 1px solid #d2d2d2;
        color: #303030;
        height: 26px;
        line-height: 26px;
    }
</style>
</head>
<body>
<include file="Public/common_header"/>
<div class="mian">
    <include file="Public/sidebar" menu='zhgl.cszh'/>
    <div class="mianR">
        <h3 class="yonghu">新建车商</h3>
        <div class="new-dealer">
            <table class="table">
                <tbody>
                <tr>
                    <td width="15%">手机号</td>
                    <td class="dealer-phone">
                        <input id="dealer_phone" type="text" value="" placeholder="请输入手机号">
                    </td>
                </tr>
                <tr>
                    <td width="15%">经营城市</td>
                    <td class="dealer-phone">
                        <select id="business_city" name="business_city" style="color: rgb(169, 169, 169);">
                            <option value="0">请选择车商经营城市</option>
                            <volist name="city_list" id="cl">
                                <option value="{$cl.code}" bc-id="{$cl.branch_id}">{$cl.name}</option>
                            </volist>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="15%">客户经理</td>
                    <td class="dealer-phone dealer-phone1">
                        <select id="branch_company" name="branch_company" style="color: rgb(169, 169, 169);">
                            <option value="-1">请选择分公司</option>
                            <option value="0">总部</option>
                            <?php foreach($branch_list as $bk=>$branch):?>
                            <option value="{$branch.id}">{$branch.name}</option>
                            <?php endforeach;?>
                        </select>
                        <select id="man_id" name="manager_id">
                            <option value="0">请选择客户经理</option>
                        </select>
                    </td>
                </tr>
                </tbody>
            </table>
            <div id="showOption" style="display:none">
                <volist name="man_list" id="vo">
                    <option class="{$vo['branch_id']}" value="{$vo['emp_id']}">{$vo['real_name']}</option>
                </volist>
            </div>
            <div class="yongBtn">
                <input type="button" value="确定" class="queding dealer-cont-btn"/>
                <input name="重置" type="reset" class="quxiao" onclick="history.go(-1);" value="取消"/>
            </div>
        </div>
    </div>
    <div class="clear"></div>
</div>
<script type="text/javascript">
    $(function () {
        init();
        //城市所属的分公司
//        $("#business_city").change(function () {
//            var bc_id = $(this).find('option:selected').attr('bc-id');
//            $("#man_id option").each(function () {
//                if ($(this).val() != '0') {
//                    $(this).remove();
//                }
//            });
//            var branch_company_obj = $("#branch_company option");
//            branch_company_obj.each(function (i, data) {
//                var branch_id = $(this).val();
//                if (bc_id == branch_id) {
//                    $(this).prop('selected', 'selected');
//                    //显示具体渠道下拉列表
//                    $("#man_id").empty()
//                            .append('<option value="0" >请选择客户经理</option>')
//                            .append($("#showOption ." + bc_id).clone());
//                } else {
//                    $(this).prop('selected', false);
//                }
//            })
//        });
        //分公司下的客户经理
        $("#branch_company").change(function () {
            var value = $(this).val(); //获取渠道当前下拉列表选中的值
            //隐藏所有下拉列表的值
            $("#man_id option").each(function () {
                if ($(this).val() != '0') {
                    $(this).remove();
                }
            });
            //显示具体渠道下拉列表
            $("#man_id").empty()
                    .append('<option value="0" >请选择客户经理</option>')
                    .append($("#showOption ." + value).clone());
        });
        //初始化分公司客户经理列表
        function init() {
            var branch_obj = $('#branch_company');
            var branch_id = branch_obj.val();
            //显示具体渠道下拉列表
            $("#man_id").empty()
                    .append('<option value="0" >请选择客户经理</option>')
                    .append($("#showOption ." + branch_id).clone());
        }

        $(".queding").click(function () {
            var dealer_phone = $('#dealer_phone').val();
            if(dealer_phone == ''){
                shibai_alert('手机号码不能为空!');
                return false;
            }
            var reg_phone = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/;
            if(!reg_phone.test(dealer_phone)){
                shibai_alert('请输入正确的手机号码');
                return false;
            }
            var business_city = $('#business_city').val();
            if(business_city < 1){
                shibai_alert('请选择经营城市');
                return false;
            }
            var branch_company = $('#branch_company').val();
            var man_id = $('#man_id').val();
            var post_data = {
                'dealer_phone': dealer_phone,
                'business_city': business_city,
                'branch_company': branch_company,
                'man_id': man_id
            }
            var btn_obj = $(this);
            btn_obj.val("提交中...").attr("disabled", 'disabled');
            $.post("/CarDealer/save_add", post_data, function (data) {
                if (data.code != 200) {
                    shibai_alert(data.msg, 1500);
                    btn_obj.val("确定").removeAttr("disabled style");
                    return false;
                }
                btn_alert("添加成功", 1500, function () {
                    location.href = "/CarDealer/user_list";
                });
            }, "json");
        });
    });
</script>
<include file="Public/foot"/>